<?php
/************************FoxPHP威客系统************************/
/*                                                            */
/*  FoxPHP.Com (C)2006-2016 FoxPHP Inc.                       */
/*  This is NOT a freeware, use is subject to license terms   */
/*  FoxPHP威客系统是商业软件,使用于商业用途请购买授权         */
/*  V3.0 2016.3                                               */
/*  foxphp@aliyun.com                                         */
/*  http://www.foxphp.com                                     */
/*                                                            */
/**************************************************************/

namespace Admin\Model;
use Think\Model;

class AdminModel extends Model {
	
	//数据列表-----------------------------------------------------------------------------------------------
	//排序,筛选,排序条件,显示页数
	function IndexGetData($orderarr=array(),$wherearr=array(),$order='id ASC',$showpage=10,$sqlwhere=null){
		
		//筛选-------------------------------
		$wheresql=$selectarray='';

		if(intval($_GET['search'])){

			$wheresqlarr=SetWhere($_GET,$wherearr);

			$wheresql=$wheresqlarr[0];
			
			$selectarray=$wheresqlarr[1];
			
			$sqlkey=$wheresqlarr[3];

		}
		
		if(!empty($wheresql) && $sqlwhere){
			$wheresql.=' AND '.$sqlwhere[0];
		}elseif(!empty($sqlwhere[0])){
			$wheresql=$sqlwhere[0];
		}
		
		if(!empty($selectarray) && $sqlwhere){
			$selectarray=array_merge($selectarray,$sqlwhere[1]);
		}elseif(!empty($sqlwhere[1])){
			$selectarray=$sqlwhere[1];
		}

		//排序-------------------------------
		$iforder=I('get.iforder',1,'int');

		if($iforder==1){
			$selectorder=' ASC';
			$iforder=2;
		}else{
			$selectorder=' DESC';
			$iforder=1;
		}
		
		$sqlorder=I('get.sqlorder',0,'int');
		foreach($orderarr as $key=>$value){
			if($sqlorder==$key){
				$order=$value['order'].$selectorder;
			}
		}

		//数据-------------------------------
		$pagenum=I('get.p',0,'int');
		$list=$this->where($wheresql,$selectarray)->order($order)->page($pagenum,$showpage)->select();

		//页码-------------------------------
		$count=$this->where($wheresql,$selectarray)->count();
		$Page = new \Think\Page($count,$showpage);

		return array('list'=>$list,'page'=>$Page->show(),'count'=>$count,'orderarr'=>$orderarr,'iforder'=>$iforder,'sqlorder'=>$sqlorder,'pagenum'=>$pagenum,'selectarray'=>$selectarray,'sqlkey'=>$sqlkey);
	}
	
	//数据添加-----------------------------------------------------------------------------------------------
	function CreateData($data=array()){
		if ($this->create($data)){
			return array('id'=>$this->add(),'message'=>'添加成功!'.$this->getError());
		}else{
			return array('id'=>0,'message'=>'添加失败!'.$this->getError());
		}
	}
	
	//数据修改-----------------------------------------------------------------------------------------------
	function EditData($data=array()){
		if($this->where(" id=$data[id] ")->save($data)){
			return array('id'=>$data['id'],'message'=>'修改成功!'.$this->getError());
		}else{
			return array('id'=>0,'message'=>'修改失败!'.$this->getError());
		}
	}
	
	//数据删除-----------------------------------------------------------------------------------------------
	function DeleteData($field='id'){

		if(IsPost('Delete') && $ids=GetCheckBox($_POST['checkbox'])){
			
		}elseif(!IsPost('Delete') && $ids=intval($_GET['id'])){

		}else{
			return array('status'=>false,'message'=>'对不起，请选择ID操作!');
		}	

		if($this->where(" $field IN($ids) ")->delete()){
			return array('status'=>true,'message'=>'删除ID '.$ids.'成功!');
		}else{
			return array('status'=>false,'message'=>'删除ID '.$ids.'失败!');
		}
		
	}
	
}

?>